package model

// Tree 泛型树形结构
type Tree[T any] struct {
	Node     *T         `json:"node,omitempty"`
	Children []*Tree[T] `json:"children,omitempty"`
}

// NewTree 创建一个新的树节点
func NewTree[T any](node *T) *Tree[T] {
	return &Tree[T]{
		Node:     node,
		Children: make([]*Tree[T], 0),
	}
}

// AddChild 添加子节点
func (t *Tree[T]) AddChild(child *Tree[T]) {
	if t.Children == nil {
		t.Children = make([]*Tree[T], 0)
	}
	t.Children = append(t.Children, child)
}

// IsLeaf 判断是否为叶子节点
func (t *Tree[T]) IsLeaf() bool {
	return len(t.Children) == 0
}
